﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using EvaluationSystem.DAO;
using Ext.Net;
using System.Text;
using EvaluationSystem.Exceptions;

namespace EvaluationSystem.ConfigurationPages.TabsPages
{
    public partial class TabsAddingPage : System.Web.UI.Page
    {
        TabsManager tabsManager = null;
        StaffManager staffManager = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Utility.IsLogin(this.Page))
            {
#if !DEBUG
                Response.Redirect("../Default.aspx");
#endif
            }
            if (tabsManager == null)
            {
                tabsManager = new TabsManager();
            }
            if (staffManager == null)
            {
                staffManager = new StaffManager();
            }
            if (!X.IsAjaxRequest)
            {
                InitRoles();
            }
        }

        private void InitRoles()
        {
            StringBuilder sbToolTip = new StringBuilder();
            DataTable dt = staffManager.GetRoles();
            Ext.Net.TreeNode root = new Ext.Net.TreeNode();
            foreach (DataRow row in dt.Rows)
            {
                Ext.Net.TreeNode node = new Ext.Net.TreeNode(row[0].ToString(), row[1] as string, Icon.ArrowJoin);
                node.Checked = ThreeStateBool.False;
                node.Leaf = true;
                root.Nodes.Add(node);

                sbToolTip.Append(String.Format("<table><tr><td width=70>{0}</td><td width=200>{1}</td></tr></table>", row[1] as string, row[2] as string));

            }
            tpRole.Root.Add(root);
            tooltipForRole.Html = sbToolTip.ToString();
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            try
            {
                bool isSuccess = tabsManager.AddTab(txtTabName.Text.Trim(), ddfRole.Text.Trim());
                if (isSuccess)
                {
                    X.MessageBox.Notify(Constants.Noticfy, "增加页签成功.").Show();
                }
                else
                {
                    X.MessageBox.Alert(Constants.Noticfy, "保存出错，请重新尝试或联系开发人员.").Show();
                }
            }
            catch (ExistTabException ex)
            {
                X.MessageBox.Alert(Constants.Noticfy, ex.Message).Show();
                txtTabName.Focus();
                txtTabName.SelectText(0);
            }
        }
    }
}